package com.ctsims.system.service.impl;

import java.util.List;
import com.ctsims.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ctsims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ctsims.system.domain.CtsStu;
import com.ctsims.system.mapper.CtsClsMapper;
import com.ctsims.system.domain.CtsCls;
import com.ctsims.system.service.ICtsClsService;

/**
 * 班级Service业务层处理
 * 
 * @author hhz
 * @date 2023-03-30
 */
@Service
public class CtsClsServiceImpl implements ICtsClsService 
{
    @Autowired
    private CtsClsMapper ctsClsMapper;

    /**
     * 查询班级
     * 
     * @param clsId 班级主键
     * @return 班级
     */
    @Override
    public CtsCls selectCtsClsByClsId(Long clsId)
    {
        return ctsClsMapper.selectCtsClsByClsId(clsId);
    }

    /**
     * 查询班级列表
     * 
     * @param ctsCls 班级
     * @return 班级
     */
    @Override
    public List<CtsCls> selectCtsClsList(CtsCls ctsCls)
    {
        return ctsClsMapper.selectCtsClsList(ctsCls);
    }

    /**
     * 新增班级
     * 
     * @param ctsCls 班级
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCtsCls(CtsCls ctsCls)
    {
        ctsCls.setCreateTime(DateUtils.getNowDate());
        int rows = ctsClsMapper.insertCtsCls(ctsCls);
        insertCtsStu(ctsCls);
        return rows;
    }

    /**
     * 修改班级
     * 
     * @param ctsCls 班级
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCtsCls(CtsCls ctsCls)
    {
        ctsCls.setUpdateTime(DateUtils.getNowDate());
        ctsClsMapper.deleteCtsStuByStuId(ctsCls.getClsId());
        insertCtsStu(ctsCls);
        return ctsClsMapper.updateCtsCls(ctsCls);
    }

    /**
     * 批量删除班级
     * 
     * @param clsIds 需要删除的班级主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCtsClsByClsIds(Long[] clsIds)
    {
        ctsClsMapper.deleteCtsStuByStuIds(clsIds);
        return ctsClsMapper.deleteCtsClsByClsIds(clsIds);
    }

    /**
     * 删除班级信息
     * 
     * @param clsId 班级主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCtsClsByClsId(Long clsId)
    {
        ctsClsMapper.deleteCtsStuByStuId(clsId);
        return ctsClsMapper.deleteCtsClsByClsId(clsId);
    }

    /**
     * 新增学生信息信息
     * 
     * @param ctsCls 班级对象
     */
    public void insertCtsStu(CtsCls ctsCls)
    {
        List<CtsStu> ctsStuList = ctsCls.getCtsStuList();
        Long clsId = ctsCls.getClsId();
        if (StringUtils.isNotNull(ctsStuList))
        {
            List<CtsStu> list = new ArrayList<CtsStu>();
            for (CtsStu ctsStu : ctsStuList)
            {
                ctsStu.setStuId(clsId);
                list.add(ctsStu);
            }
            if (list.size() > 0)
            {
                ctsClsMapper.batchCtsStu(list);
            }
        }
    }
}
